Skip to contentSkip to conversation

Change in openvpn[master]: buffer: Change limits for array_mult_safe

Openvpn-devel In review flichtenheld (Code Review) 2 participants 6 messages Dec 11, 2025 – Dec 12, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1765467748000.I4f95edd7ce2098180aa620a231727217f333a12d@...2715...

#1 lore raw

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email

to review the following change.

Change subject: buffer: Change limits for array_mult_safe
......................................................................

buffer: Change limits for array_mult_safe

- Lower the limit to 1GB on 32bit systems.
The limit of 4GB-1 makes no sense on systems that usually don't allow a single process to allocate anything near to this limit.
- Increate the limit from 4GB-1 to 4GB on other systems. It makes no difference in protection but makes it much easier to use the limit in other contexts, e.g. if dividing it.

Change-Id I4f95edd7ce2098180aa620a231727217f333a12d

---
M src/openvpn/buffer.c
M src/openvpn/buffer.h

2 files changed, 13 insertions(+), 1 deletion(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/36/1436/1

src/openvpn/buffer.c +1 -1
index 293622f..40baca6 100644
@@ -39,7 +39,7 @@
 size_t
 array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
 {
-    const size_t limit = 0xFFFFFFFF;
+    const size_t limit = ALLOC_SIZE_MAX;
     unsigned long long res =
         (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
     if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit)
src/openvpn/buffer.h +12 -0
index ab2a29d..1dbe0b2 100644
@@ -1044,6 +1044,18 @@
  * Allocate memory to hold a structure
  */
 
+/* When allocating arrays make sure we do not use a excessive amount
+ * of memory.
+ */
+#if UINTPTR_MAX <= UINT32_MAX
+/* 1 GB on 32bit systems, they usually can only allocate 2 GB for the
+ * whole process.
+ */
+#define ALLOC_SIZE_MAX (1u << 30)
+#else
+#define ALLOC_SIZE_MAX ((size_t)1 << 32) /* 4 GB */
+#endif
+
 #define ALLOC_OBJ(dptr, type)                                       \
     {                                                               \
         check_malloc_return((dptr) = (type *)malloc(sizeof(type))); \

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I4f95edd7ce2098180aa620a231727217f333a12d
Gerrit-Change-Number: 1436
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

cron2 has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email )

Change subject: buffer: Change limits for array_mult_safe
......................................................................

Patch Set 1: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I4f95edd7ce2098180aa620a231727217f333a12d
Gerrit-Change-Number: 1436
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Fri, 12 Dec 2025 10:08:59 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes

#3 lore raw

PATCH 1/1 buffer: Change limits for array_mult_safe

In reply to a message not in the archive

From: Frank Lichtenheld <[email protected]...>

- Lower the limit to 1GB on 32bit systems.
The limit of 4GB-1 makes no sense on systems that usually don't allow a single process to allocate anything near to this limit.
- Increate the limit from 4GB-1 to 4GB on other systems. It makes no difference in protection but makes it much easier to use the limit in other contexts, e.g. if dividing it.

Change-Id I4f95edd7ce2098180aa620a231727217f333a12d

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1436
---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1436
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>

src/openvpn/buffer.c +1 -1
index 293622f..40baca6 100644
@@ -39,7 +39,7 @@
 size_t
 array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
 {
-    const size_t limit = 0xFFFFFFFF;
+    const size_t limit = ALLOC_SIZE_MAX;
     unsigned long long res =
         (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
     if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit)
src/openvpn/buffer.h +12 -0
index ab2a29d..1dbe0b2 100644
@@ -1044,6 +1044,18 @@
  * Allocate memory to hold a structure
  */
 
+/* When allocating arrays make sure we do not use a excessive amount
+ * of memory.
+ */
+#if UINTPTR_MAX <= UINT32_MAX
+/* 1 GB on 32bit systems, they usually can only allocate 2 GB for the
+ * whole process.
+ */
+#define ALLOC_SIZE_MAX (1u << 30)
+#else
+#define ALLOC_SIZE_MAX ((size_t)1 << 32) /* 4 GB */
+#endif
+
 #define ALLOC_OBJ(dptr, type)                                       \
     {                                                               \
         check_malloc_return((dptr) = (type *)malloc(sizeof(type))); \
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: buffer: Change limits for array_mult_safe

As discussed on IRC, this is a reasonable change - on 32bit platforms, having a maximum allocation size of 4G-1 is no good, as that will always fail (so why bother having that check at all). 1G seems well out of range of what OpenVPN would ever need, but it *would* catch some program parts asking for "2Gbyte" and then being surprised that malloc() fails... (and provide a better FATAL message).

Not tested beyond "BB says it's all green".

Your patch has been applied to the master branch.

commit 2b8149a4fe0d73ea0bedf2e282188d55c8511c45

Author: Frank Lichtenheld
Date:   Fri Dec 12 11:09:14 2025 +0100
     buffer: Change limits for array_mult_safe

     Signed-off-by: Frank Lichtenheld <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1436
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg35044.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#5 lore raw
In reply to a message not in the archive

cron2 has uploaded a new patch set (#2) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by cron2

Change subject: buffer: Change limits for array_mult_safe
......................................................................

buffer: Change limits for array_mult_safe

- Lower the limit to 1GB on 32bit systems.
The limit of 4GB-1 makes no sense on systems that usually don't allow a single process to allocate anything near to this limit.
- Increate the limit from 4GB-1 to 4GB on other systems. It makes no difference in protection but makes it much easier to use the limit in other contexts, e.g. if dividing it.

Change-Id I4f95edd7ce2098180aa620a231727217f333a12d

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1436
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35044.html

---
M src/openvpn/buffer.c
M src/openvpn/buffer.h

2 files changed, 13 insertions(+), 1 deletion(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/36/1436/2

src/openvpn/buffer.c +1 -1
index 293622f..40baca6 100644
@@ -39,7 +39,7 @@
 size_t
 array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
 {
-    const size_t limit = 0xFFFFFFFF;
+    const size_t limit = ALLOC_SIZE_MAX;
     unsigned long long res =
         (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
     if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit)
src/openvpn/buffer.h +12 -0
index ab2a29d..1dbe0b2 100644
@@ -1044,6 +1044,18 @@
  * Allocate memory to hold a structure
  */
 
+/* When allocating arrays make sure we do not use a excessive amount
+ * of memory.
+ */
+#if UINTPTR_MAX <= UINT32_MAX
+/* 1 GB on 32bit systems, they usually can only allocate 2 GB for the
+ * whole process.
+ */
+#define ALLOC_SIZE_MAX (1u << 30)
+#else
+#define ALLOC_SIZE_MAX ((size_t)1 << 32) /* 4 GB */
+#endif
+
 #define ALLOC_OBJ(dptr, type)                                       \
     {                                                               \
         check_malloc_return((dptr) = (type *)malloc(sizeof(type))); \

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I4f95edd7ce2098180aa620a231727217f333a12d
Gerrit-Change-Number: 1436
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>

#6 lore raw
In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email )

Change subject: buffer: Change limits for array_mult_safe
......................................................................

buffer: Change limits for array_mult_safe

- Lower the limit to 1GB on 32bit systems.
The limit of 4GB-1 makes no sense on systems that usually don't allow a single process to allocate anything near to this limit.
- Increate the limit from 4GB-1 to 4GB on other systems. It makes no difference in protection but makes it much easier to use the limit in other contexts, e.g. if dividing it.

Change-Id I4f95edd7ce2098180aa620a231727217f333a12d

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1436
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35044.html

---
M src/openvpn/buffer.c
M src/openvpn/buffer.h

2 files changed, 13 insertions(+), 1 deletion(-)
src/openvpn/buffer.c +1 -1
index 293622f..40baca6 100644
@@ -39,7 +39,7 @@
 size_t
 array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
 {
-    const size_t limit = 0xFFFFFFFF;
+    const size_t limit = ALLOC_SIZE_MAX;
     unsigned long long res =
         (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
     if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit)
src/openvpn/buffer.h +12 -0
index ab2a29d..1dbe0b2 100644
@@ -1044,6 +1044,18 @@
  * Allocate memory to hold a structure
  */
 
+/* When allocating arrays make sure we do not use a excessive amount
+ * of memory.
+ */
+#if UINTPTR_MAX <= UINT32_MAX
+/* 1 GB on 32bit systems, they usually can only allocate 2 GB for the
+ * whole process.
+ */
+#define ALLOC_SIZE_MAX (1u << 30)
+#else
+#define ALLOC_SIZE_MAX ((size_t)1 << 32) /* 4 GB */
+#endif
+
 #define ALLOC_OBJ(dptr, type)                                       \
     {                                                               \
         check_malloc_return((dptr) = (type *)malloc(sizeof(type))); \

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1436?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I4f95edd7ce2098180aa620a231727217f333a12d
Gerrit-Change-Number: 1436
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>